package com.accesshollywood.components;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.ToolTipManager;

import com.accesshollywood.grfxOrdering.GRFXOrdersMain;
import com.accesshollywood.grfxOrdering.OrderMainGUI;
import com.accesshollywood.indexables.items.TextIndexed;

public class EditableTextWithPopup extends JTextField implements HasPopupMenu {
	public BasicPopupMenu	popup		= new BasicPopupMenu(this);
	protected JComponent	parent;
	private TextIndexed		oldText		= new TextIndexed();
	private TextIndexed		currText	= new TextIndexed();
	protected Class			refreshMe	= OrderMainGUI.class;

	public EditableTextWithPopup(JComponent parent, int fontSize) {
		super();
		init(parent, fontSize);
		setupPopup();
	}

	public EditableTextWithPopup(JComponent parent, TextIndexed newText, int fontSize) {
		super();
		init(parent, fontSize);
		setupPopup();

		oldText = currText = newText;
		this.setText(newText.getStr());
	}

	public void init(JComponent parent, int fontSize) {
		this.parent = parent;
		/*
		 * getActionMap().put("startEditing", new StartEditingAction());
		 * getActionMap().put("cancel", new CancelEditingAction());
		 * getActionMap().put("stopEditing", new StopEditingAction());
		 * getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "startEditing");
		 * getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
		 * getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "stopEditing");
		 * putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
		 */
		ToolTipManager.sharedInstance().registerComponent(this);

		setFont(new Font(this.getFont().getName(), this.getFont().getStyle(), fontSize));
		this.setEditable(false);
		this.setEnabled(false);

		putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
		this.addFocusListener(new EditableFocusListener());
		this.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				int key = e.getKeyCode();
				if (key == KeyEvent.VK_ENTER) {
					stopEditing();
				}
				if (key == KeyEvent.VK_ESCAPE) {
					cancelEditing();
				}
			}
		});

	}

	public void setText(TextIndexed newText) {
		oldText = currText;
		currText = newText;
		this.setText(currText.getStr().replace("\n", ""));
	}

	@Override
	public void setupPopup() {
		popup.addMenuItem("Edit", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				EditableTextWithPopup.this.setEnabled(true);
				EditableTextWithPopup.this.setEditable(true);
			}

		});
	}

	public void startEditing() {
		oldText.setStr(currText.getStr());
	}

	public void cancelEditing() {
		setText(oldText);
		EditableTextWithPopup.this.setEditable(false);
		EditableTextWithPopup.this.setEnabled(false);
	}

	public void stopEditing() {
		currText.setStr(EditableTextWithPopup.this.getText());
		if (oldText == null || (oldText != null && !oldText.equals(currText))) {
			Integer[] id;
			currText.toSQL();
			GRFXOrdersMain.refresh("items", new int[] { oldText.getId() }, true);
			// model.p2pRefreshEntry(oldText.getId());
		}
		EditableTextWithPopup.this.setEditable(false);
		EditableTextWithPopup.this.setEnabled(false);
	}

	public class EditableFocusListener implements FocusListener {

		@Override
		public void focusGained(FocusEvent fe) {
			startEditing();
			// oldText.setStr(currText.getStr());
			// oldText.setStr(EditableTextWithPopup.this.getText());
		}

		@Override
		public void focusLost(FocusEvent fe) {
			// currText.setStr(EditableTextWithPopup.this.getText());
			// if (oldText == null || (oldText != null && !oldText.equals(currText))) {
			// Integer[] id;
			// currText.toSQL();
			// GRFXOrdersMain.refresh("items", new int[] { oldText.getId() }, true);
			// // model.p2pRefreshEntry(oldText.getId());
			// }
			/*
			 * if (editable) {
			 * int row = getSelectedRow();
			 * int col = 0;
			 * if (row > -1) {
			 * editable = true;
			 * IndexedSQL newEntry;
			 * Object whatsHere = getValueAt(row, col);
			 * if ((whatsHere instanceof String)) {
			 * int oldTextID = oldText.getID();
			 * newEntry = new IndexedSQL(oldTextID, (String) whatsHere);
			 * } else {
			 * newEntry = (IndexedSQL) whatsHere;
			 * }
			 * if (oldText == null || (oldText != null && !oldText.equals(newEntry))) {
			 * Integer[] id;
			 * GFXSQL.alterString(model.sqlTableName, model.sqlEntryColumnName, newEntry.toString(), newEntry.getID());
			 * GRFXOrdersMain.refresh("items", new int[] { oldText.getID() }, true);
			 * model.p2pRefreshEntry(oldText.getID());
			 * }
			 * editable = false;
			 * }
			 * }
			 */
		}

	}
}
